<?php 

class DvrApi{
	private $accessId;
	private $host = 'http://openapi.aodianyun.com';
	private $accessKey;
	public function __construct($accessId,$accessKey) {
		$this->accessKey = $accessKey;
		$this->accessId = $accessId;
	}
	
	public function get7DayDvrList($param){
		
		$content = json_encode(array('access_id' =>  $this->accessId,
		                             'access_key' =>  $this->accessKey,
		                             'app' =>  $param['app'],
		                             'stream' =>  $param['stream']));
		
		return $this->curl("/v2/DVR.Get7DayDvrList","POST",$content);
	}
	private function curl($path,$method="GET",$content="",$expire=3600,$timeout=60){
		$ch = curl_init();
// 		$expire = time() + $expire;
// 		if($method == "POST" && !empty($content)){
// 			$contentMd5 = md5($content);
// 			$str = $method."\n".$path."\n".$expire."\n".$contentMd5."\n";
// 		}else{
// 			$str = $method."\n".$path."\n".$expire."\n";
// 		}
		$query_url = $this->host.$path;

		curl_setopt($ch,CURLOPT_URL,$query_url);
		curl_setopt($ch,CURLOPT_HEADER,false);
		
		curl_setopt($ch,CURLOPT_AUTOREFERER,true);
		curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
		curl_setopt($ch,CURLOPT_FRESH_CONNECT,true);
		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
		curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
		//	curl_setopt($ch,CURLOPT_USERAGENT,$useragent);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
		curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$method);
	       // curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		if($method == 'POST') {
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$content);
		}
		//$authorization = 'Authorization: wis '.$this->Sign($str);
		//print_r($authorization);exit();
		$header = array('Content-Type: application/x-www-form-urlencoded');
		curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
		//execute post
		$response = curl_exec($ch);
		//get response code
		$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		//close connection 
		curl_close($ch);
		//return result
		
		if($response_code == 200 || $response_code == 201 || $response_code == 204) {
			
			$rst = array(
				'Flag'=>100,
				'FlagString'=>'success',
				'Info'=>json_decode(trim($response),true)
			);
		} else {
		    //print_r('$response:'.trim($response));
			$rst = array(
				'Flag'=>110,
				'FlagString'=>'faile',
				'Info'=>trim($response)
			);
			/*
			$rr = json_decode(trim($response),true);
			$rst = array(
				'Flag'=>$rr['code'],
				'FlagString'=>$rr['error']
			);*/
		}
		return $rst;
	}
	
}
?>